package com.run.guifeng.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mysql.jdbc.StringUtils;
import com.run.guifeng.dao.GoodsInfoMapper;
import com.run.guifeng.dao.IndexConfigMapper;
import com.run.guifeng.model.entity.GoodsInfo;
import com.run.guifeng.model.entity.IndexConfig;
import com.run.guifeng.model.enums.ServiceResultEnum;
import com.run.guifeng.model.vo.IndexConfigGoodsVO;
import com.run.guifeng.service.IndexConfigService;
import com.run.guifeng.util.BeanUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class IndexConfigImplService implements IndexConfigService {

    @Resource
    private IndexConfigMapper indexConfigMapper;

    @Resource
    private GoodsInfoMapper goodsInfoMapper;

    @Override
    public List<IndexConfig> getConfigsPage(Integer configType) {
        List<IndexConfig> indexConfigs = indexConfigMapper.selectList(Wrappers.<IndexConfig>lambdaQuery()
                .eq(configType != null && !StringUtils.isNullOrEmpty(configType.toString()), IndexConfig::getConfigType, configType)
                .eq(IndexConfig::getDeleted, false)
        );
        return indexConfigs;
    }

    @Override
    public String saveIndexConfig(IndexConfig indexConfig) {
        if (indexConfigMapper.insert(indexConfig) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        } else {
            return ServiceResultEnum.DB_ERROR.getResult();
        }
    }

    @Override
    public String updateIndexConfig(IndexConfig indexConfig) {
        IndexConfig temp = indexConfigMapper.selectById(indexConfig.getId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        indexConfig.setUpdateTime(new Date());
        if (indexConfigMapper.updateById(indexConfig) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public IndexConfig getIndexConfigById(Integer id) {
        IndexConfig indexconfig = indexConfigMapper.selectById(id);
        if (indexconfig != null) {
            return indexconfig;
        }
        return null;
    }

    @Override
    public List<IndexConfigGoodsVO> getConfigGoodsesForIndex(int configType) {
        List<IndexConfig> indexConfigs = indexConfigMapper.selectList(Wrappers.<IndexConfig>lambdaQuery()
                .eq(IndexConfig::getConfigType, configType));
        List<Integer> indexConfigsGoodsId = indexConfigs.stream().map(IndexConfig::getGoodsId).collect(Collectors.toList());
        return BeanUtil.copyList(goodsInfoMapper.selectList(Wrappers.<GoodsInfo>lambdaQuery()
                .in(GoodsInfo::getId, indexConfigsGoodsId)), IndexConfigGoodsVO.class);
    }


    @Override
    public String deleteBatch(Integer[] ids) {
        if (ids.length < 1) {
            return ServiceResultEnum.ERROR.getResult();
        }
        if (indexConfigMapper.deleteBatchIds(Arrays.asList(ids)) == ids.length) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.ERROR.getResult();
    }

}
